<?php
require_once("database.php");

class mysqlInsert {
	private $query;
	private $insertedId;
	private $databaseSelection;
	
	public function __construct($databaseSelection, $query, $table, $values){
		if($query)
			$this->query = $query;
		else
	 		$this->buildQuery($table, $values);
	 	$this->databaseSelection = $databaseSelection;
	}
	
	public function __destruct(){}
	
	public function execute(){
		$database;
		switch($this->databaseSelection){
			case "local": $database = new databaseLocal(); break;
			case "forum": $database = new databaseForum(); break;
			case "tournament": $database = new databaseTournament(); break;			 
		}
		
		$databaseResource = $database->connect();
		if($databaseResource){
			$resource = mysql_query($this->query, $databaseResource);
			$this->insertedId = mysql_insert_id();
			
			if(!$resource)
				echo mysql_errno().": ".mysql_error()."<br>".$this->query." in ".__FILE__." (".__METHOD__.", line ".__LINE__.")";
			else
				$database->disconnect();
			return TRUE;
		}
		return FALSE;
	}
	
	public function buildQuery($table, $values){		
		$this->query = "INSERT INTO `".$table."` VALUES (";
		foreach($values as $value){
			$this->query .= "'".$value."',";
		}
		$this->query = substr($this->query, 0, strlen($this->query)-1).");";
	}
	
	public function getInsertedId(){
		return $this->insertedId;
	}
}

class mysqlQuery {
	private $query;
	private $result;
	private $count;
	private $resource;
	private $databaseSelection;
	
	public function __construct($databaseSelection, $query, $table, $fields, $where, $order_field, $order){
		if($query)
			$this->query = $query;
		else 
			$this->buildQuery($table, $fields, $where, $order_field, $order);
	 	$this->databaseSelection = $databaseSelection;
	}
	
	public function __destruct(){}
	
	public function execute(){
		$database;
		switch($this->databaseSelection){
			case "local": $database = new databaseLocal(); break;
			case "forum": $database = new databaseForum(); break;
			case "tournament": $database = new databaseTournament(); break;			 
		}
		//echo $this->query."<br>";
		$databaseResource = $database->connect();
		if($databaseResource){
			$this->resource = mysql_query($this->query, $databaseResource);		
			if(!$this->resource)
				echo mysql_errno($databaseResource).": ".mysql_error($databaseResource)."<br>".$this->query."  in ".__FILE__." (".__METHOD__.", line ".__LINE__.")";	
			else
				$this->count = mysql_num_rows($this->resource);
			$database->disconnect();
			return TRUE;
		}
		return FALSE;
	}
	
	public function hasNext(){
		if($this->result)
			return TRUE;
		return FALSE;
	}
	
	public function next(){
		return $this->result = mysql_fetch_array($this->resource, MYSQL_ASSOC);
	}
	
	public function getCount(){
		return $this->count;
	}
	
	public function buildQuery($table, $fields, $where, $order_field, $order){
		$this->query = "SELECT ";
		if(strlen($fields) == 0 OR strlen($fields == "*"))
			$this->query .= "* FROM `".$table."`";
		else
			$this->query .= $fields." FROM `".$table."`";		
		if(strlen($where) != 0)
			$this->query .= " WHERE ".$where;
		if(strlen($order_field) != 0)
			$this->query .= " ORDER BY `".$order_field."`";
		if(strlen($order) != 0)
			$this->query .= " ".$order;
		$this->query .= ";";
	}
}

class mysqlUpdate{
	private $query;	
	private $databaseSelection;
	
	public function __construct($databaseSelection, $query, $table, $fields, $values, $where){
		if($query)
			$this->query = $query;
		else
			$this->buildQuery($table, $fields, $values, $where);
		$this->databaseSelection = $databaseSelection;
	}
	
	public function buildQuery($table, $fields, $values, $where){
		if(sizeof($fields) == sizeof($values)){
			$this->query = "UPDATE `".$table."` SET ";
			for($i = 0; $i < sizeof($fields); $i++)
				$this->query .= "`".$fields[$i]."`='".$values[$i]."',";
			$this->query = substr($this->query, 0, strlen($this->query)-1)."";
			if($where)
				$this->query .= " WHERE ".$where;
			$this->query .= ";";
		}
		else{
			echo "Error on building Update Query -> fields and values are not the same size arrays\n";
		} 				
	}
	
	public function execute(){
		switch($this->databaseSelection){
			case "local": $database = new databaseLocal(); break;
			case "forum": $database = new databaseForum(); break;
			case "tournament": $database = new databaseTournament(); break;			 
		}
		//echo $this->query."<br>";
		$databaseResource = $database->connect();
		if($databaseResource){
			$esource = mysql_query($this->query, $databaseResource);		
			if(!$esource)
				echo mysql_errno($databaseResource).": ".mysql_error($databaseResource)."<br>".$this->query."  in ".__FILE__." (".__METHOD__.", line ".__LINE__.")";			
			else
				$database->disconnect();
			return TRUE;
		}
		return FALSE;
	}
}

class mysqlDelete{
	private $query;
	private $databaseSelection;
	
	public function __construct($databaseSelection, $query, $table, $where){
		if($query)
			$this->query = $query;
		else
			$this->buildQuery($table, $where);
		$this->databaseSelection = $databaseSelection;
	}
	
	public function buildQuery($table, $where){
		if($where)
			$this->query = "DELETE FROM `".$table."` WHERE ".$where.";";
		else
			$this->query = "DELETE FROM `".$table."`;";
	}
	
	public function execute(){	
		switch($this->databaseSelection){
			case "local": $database = new databaseLocal(); break;
			case "forum": $database = new databaseForum(); break;
			case "tournament": $database = new databaseTournament(); break;			 
		}
		$databaseResource = $database->connect();
		if($databaseResource){
			$resource = mysql_query($this->query, $databaseResource);
			if(!$resource)
				echo mysql_errno($databaseResource).": ".mysql_error($databaseResource)."<br>".$this->query."  in ".__FILE__." (".__METHOD__.", line ".__LINE__.")";
			else
				$database->disconnect();
			return TRUE;
		}
		return FALSE;
	}
}

?>